package com.pristine.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.collections.CollectionUtils;

import com.pristine.domain.CategoryTypeMasterEntity;
import com.pristine.domain.ConsumableEntity;
import com.pristine.domain.CountryMasterEntity;
import com.pristine.domain.CustomerEntity;
import com.pristine.domain.DepartmentMasterEntity;
import com.pristine.domain.DoctorMasterEntity;
import com.pristine.domain.EmployeeEntity;
import com.pristine.domain.EquipmentEntity;
import com.pristine.domain.MedicineEntity;
import com.pristine.domain.OperatorEntity;
import com.pristine.domain.PatientEntity;
import com.pristine.domain.PhoneNumberEntity;
import com.pristine.domain.StateMasterEntity;
import com.pristine.domain.TreatmentPackageEntity;
import com.pristine.service.wsdl.CategoryTypeMasterVO;
import com.pristine.service.wsdl.ConsumableVO;
import com.pristine.service.wsdl.CountryMasterVO;
import com.pristine.service.wsdl.CustomerVO;
import com.pristine.service.wsdl.DepartmentMasterVO;
import com.pristine.service.wsdl.DoctorMasterVO;
import com.pristine.service.wsdl.EmployeeVO;
import com.pristine.service.wsdl.EquipmentVO;
import com.pristine.service.wsdl.MedicineVO;
import com.pristine.service.wsdl.OperatorVO;
import com.pristine.service.wsdl.PatientVO;
import com.pristine.service.wsdl.PhoneNumberVO;
import com.pristine.service.wsdl.StateMasterVO;
import com.pristine.service.wsdl.TreatmentPackageVO;

public class VOToEntityConverter {
	/*
	 * Converts XMLGregorianCalendar to java.util.Date in Java
	 */
	public static Date toDate(XMLGregorianCalendar calendar) {
		if (calendar == null) {
			return null;
		}
		return calendar.toGregorianCalendar().getTime();
	}

	public static CategoryTypeMasterEntity convert(CategoryTypeMasterVO vo) {
		CategoryTypeMasterEntity entity = new CategoryTypeMasterEntity();
		entity.setCatTypeId(vo.getCatTypeId());
		entity.setCatTypeName(vo.getCatTypeName());
		entity.setCreatedBy(vo.getCreatedBy());
		entity.setModifiedBy(vo.getModifiedBy());
		if (vo.getCreatedOn() != null)
			entity.setCreatedOn(toDate(vo.getCreatedOn()));
		
		if (vo.getModifiedOn() != null)
			entity.setModifiedOn(toDate(vo.getModifiedOn()));
		
		entity.setStatus(vo.getStatus());
		return entity;
	}

	public static DepartmentMasterEntity convert(DepartmentMasterVO vo) {
		DepartmentMasterEntity entity = new DepartmentMasterEntity();
		entity.setDeptId(vo.getDeptId());
		entity.setDeptName(vo.getDeptName());
		return entity;
	}

	public static DoctorMasterEntity convert(DoctorMasterVO vo) {
		DoctorMasterEntity entity = new DoctorMasterEntity();
		entity.setActive(vo.getActive());
		if (vo.getCategoryType() != null)
			entity.setCategoryType(VOToEntityConverter.convert(vo
					.getCategoryType()));
		entity.setContactNo(vo.getContactNo());
		entity.setCreatedBy(vo.getCreatedBy());
		entity.setDegree(vo.getDegree());
		entity.setFirstName(vo.getFirstName());
		entity.setId(vo.getId());
		entity.setLastName(vo.getLastName());
		entity.setMiddleName(vo.getMiddleName());
		entity.setModifiedBy(vo.getModifiedBy());
		
		if (vo.getCreatedOn() != null)
			entity.setCreatedOn(toDate(vo.getCreatedOn()));
		
		if (vo.getModificationOn() != null)
			entity.setModificationOn(toDate(vo.getModificationOn()));
		
		entity.setSpeciality(vo.getSpeciality());

		DepartmentMasterVO departmentVO = vo.getDepartment();
		entity.setDepartment(VOToEntityConverter.convert(departmentVO));

		return entity;
	}

	public static MedicineEntity convert(MedicineVO vo) {
		MedicineEntity entity = new MedicineEntity();
		entity.setBatchNo(vo.getBatchNo());
		entity.setCreatedBy(vo.getCreatedBy());
		entity.setId(vo.getMedicineId());
		entity.setMedicineName(vo.getMedicineName());
		entity.setModifiedBy(vo.getModifiedBy());
		entity.setPower(vo.getPower());
		entity.setRate(vo.getRate());
		entity.setStatus(vo.getStatus());

		if (vo.getCreatedOn() != null)
			entity.setCreatedOn(toDate(vo.getCreatedOn()));
		
		if (vo.getModifiedOn() != null)
			entity.setModificationOn(toDate(vo.getModifiedOn()));
		
		
		if (vo.getCategoryType() != null)
			entity.setCategoryType(VOToEntityConverter.convert(vo
					.getCategoryType()));
		return entity;
	}

	public static CustomerEntity convert(CustomerVO customerVO) {
		CustomerEntity entity = new CustomerEntity();
		entity.setCustomerName(customerVO.getCustomerName());
		entity.setId(customerVO.getId());

		List<PhoneNumberEntity> list = new ArrayList<PhoneNumberEntity>();
		List<PhoneNumberVO> volist = customerVO.getPhones();
		if (CollectionUtils.isNotEmpty(volist)) {
			for (PhoneNumberVO phone : volist) {
				PhoneNumberEntity phoneentity = convert(phone);
				list.add(phoneentity);
			}
		}
		entity.setPhones(list);
		return entity;
	}

	public static PhoneNumberEntity convert(PhoneNumberVO phoneVO) {
		PhoneNumberEntity entity = new PhoneNumberEntity();
		entity.setId(phoneVO.getId());
		entity.setPhNumber(phoneVO.getPhNumber());
		List<CustomerEntity> list = new ArrayList<CustomerEntity>();
		List<CustomerVO> volist = phoneVO.getCustomers();
		if (CollectionUtils.isNotEmpty(volist)) {
			for (CustomerVO vo : volist) {
				list.add(VOToEntityConverter.convert(vo));
			}
		}
		entity.setCustomers(list);
		return entity;
	}

	public static EmployeeEntity convert(EmployeeVO employeeVO) {
		EmployeeEntity employee = new EmployeeEntity();
		employee.setAge(employeeVO.getAge());
		employee.setId(employeeVO.getId());
		employee.setName(employeeVO.getName());

		return employee;
	}

	public static CountryMasterEntity convert(CountryMasterVO masterVO) {
		CountryMasterEntity entity = new CountryMasterEntity();
		entity.setCountryName(masterVO.getCountryName());
		entity.setId(masterVO.getId());
		List<StateMasterEntity> stateMasters = new ArrayList<StateMasterEntity>();

		List<StateMasterVO> stateVOList = masterVO.getStateMasters();
		if (CollectionUtils.isNotEmpty(stateVOList)) {
			for (StateMasterVO stateMasterVO : stateVOList) {
				stateMasters.add(convert(stateMasterVO));
			}
		}
		entity.setStateMasters(stateMasters);

		return entity;
	}

	public static StateMasterEntity convert(StateMasterVO masterVO) {
		StateMasterEntity entity = new StateMasterEntity();
		entity.setId(masterVO.getId());
		entity.setStateName(masterVO.getStateName());
		if (masterVO.getCountryMaster() != null) {
			entity.setCountryMaster(VOToEntityConverter.convert(masterVO
					.getCountryMaster()));
		}

		return entity;
	}

	public static PatientEntity convert(PatientVO patientVO) {
		PatientEntity entity = new PatientEntity();
		entity.setAddress(patientVO.getAddress());
		entity.setAge(patientVO.getAge());
		entity.setContactNo(patientVO.getContactNo());
		entity.setCountry(patientVO.getCountry());
		entity.setCreatedBy(patientVO.getCreatedBy());
		entity.setFirstName(patientVO.getFirstName());
		entity.setLastName(patientVO.getLastName());
		entity.setMaritalstatus(patientVO.getMaritalstatus());
		entity.setMiddleName(patientVO.getMiddleName());
		entity.setModifiedBy(patientVO.getModifiedBy());
		entity.setNationality(patientVO.getNationality());
		entity.setReligion(patientVO.getReligion());
		entity.setSex(patientVO.getSex());
		entity.setState(patientVO.getState());
		entity.setStatus(patientVO.getStatus());
		entity.setUhid(patientVO.getUhid());
		
		if (patientVO.getCreatedOn() != null)
			entity.setCreatedOn(toDate(patientVO.getCreatedOn()));
		
		if (patientVO.getModifiedOn() != null)
			entity.setModifiedOn(toDate(patientVO.getModifiedOn()));
		
		return entity;
	}

	public static EquipmentEntity convert(EquipmentVO equipmentVO) {
		EquipmentEntity entity = new EquipmentEntity();
		if (equipmentVO.getCategoryType() != null)
			entity.setCategoryType(VOToEntityConverter.convert(equipmentVO
					.getCategoryType()));

		if (equipmentVO.getCreatedBy() != null) {
			entity.setCreatedBy(equipmentVO.getCreatedBy());
		}
		if (equipmentVO.getCategoryType() != null)
			entity.setCategoryType(VOToEntityConverter.convert(equipmentVO
					.getCategoryType()));

		if (equipmentVO.getEquipmentId() != null) {
			entity.setEquipmentId(equipmentVO.getEquipmentId());
		}

		entity.setEquipmentName(equipmentVO.getEquipmentName());
		if (equipmentVO.getModifiedBy() != null) {
			entity.setModifiedBy(equipmentVO.getModifiedBy());
		}

		if (equipmentVO.getCreatedOn() != null)
			entity.setCreatedOn(toDate(equipmentVO.getCreatedOn()));
		
		if (equipmentVO.getModifiedOn() != null)
			entity.setModifiedOn(toDate(equipmentVO.getModifiedOn()));
		
		entity.setPrice(equipmentVO.getPrice());
		entity.setStatus(equipmentVO.getStatus());
		return entity;
	}

	public static ConsumableEntity convert(ConsumableVO consumableVO) {
		if (consumableVO == null) {
			return null;
		}

		ConsumableEntity entity = new ConsumableEntity();
		if (consumableVO.getCategoryType() != null)
			entity.setCategoryType(VOToEntityConverter.convert(consumableVO
					.getCategoryType()));
		if (consumableVO.getConsumableId() != null)
			entity.setConsumableId(consumableVO.getConsumableId());
		entity.setConsumableName(consumableVO.getConsumableName());
		entity.setConsumablePrice(consumableVO.getConsumablePrice());
		entity.setCreatedBy(consumableVO.getCreatedBy());
		entity.setModifiedBy(consumableVO.getModifiedBy());

		if (consumableVO.getCreatedOn() != null)
			entity.setCreatedOn(toDate(consumableVO.getCreatedOn()));
		
		if (consumableVO.getModifiedOn() != null)
			entity.setModifiedOn(toDate(consumableVO.getModifiedOn()));
		
		entity.setStatus(consumableVO.getStatus());
		return entity;
	}

	public static OperatorEntity convert(OperatorVO operatorVO) {
		if (operatorVO == null)
			return null;

		OperatorEntity entity = new OperatorEntity();
		if (operatorVO.getOperatorId() != null)
			entity.setOperatorId(operatorVO.getOperatorId());
		entity.setOperatorName(operatorVO.getOperatorName());
		entity.setPhoneNo(operatorVO.getPhoneNo());
		return entity;
	}

	public static TreatmentPackageEntity convert(
			TreatmentPackageVO treatmentPackageVO) {
		
		TreatmentPackageEntity entity = new TreatmentPackageEntity();
		entity.setNoOfAppointment(treatmentPackageVO.getNoOfAppointment());
		entity.setTotalAmount(treatmentPackageVO.getTotalAmount());
		if(treatmentPackageVO.getTreatmentPackageId()!=null)
			entity.setTreatmentPackageId(treatmentPackageVO.getTreatmentPackageId());
		entity.setTreatmentPackageName(treatmentPackageVO.getTreatmentPackageName());
		return entity;
	}
}
